package agent.handler.bs;

import agent.container.HttpForwardClient;
import agent.container.TcpForwardClient;
import common.body.TcpTunnelBody;
import common.enums.ProtocolEnum;
import common.handler.AbstractBsHandler;
import common.packet.BsPacket;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;

/**
 * @author gitama
 * @version 1.0
 * @date 2022/12/05
 */
public class TcpTunnelCloseReqHandler extends AbstractBsHandler<TcpTunnelBody> {

    @Override
    public Class<TcpTunnelBody> bodyClass() {
        return TcpTunnelBody.class;
    }

    @Override
    public void handler(BsPacket packet, TcpTunnelBody bsBody, ChannelContext channelContext) throws Exception {
        ChannelContext ctx;
        if (ProtocolEnum.HTTP.getCode().equals(bsBody.getProtocol())) {
            ctx = Tio.getByBsId(HttpForwardClient.clientTioConfig, bsBody.getBsId());
        } else {
            ctx = Tio.getByBsId(TcpForwardClient.clientTioConfig, bsBody.getBsId());
        }
        Tio.close(ctx, "主动被动断开");
    }

}
